package com.mall.promotion.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.promotion.constant.PromotionServiceEnum;
import com.mall.promotion.dto.bo.resp.SecSkillListResp;
import com.mall.promotion.dto.entity.TbPromoSession;
import com.mall.promotion.dto.model.SecKillListModel;
import com.mall.promotion.dto.model.SecKillProductModel;
import com.mall.promotion.dto.vo.SecKillListVo;
import com.mall.promotion.mapper.TbPromoSessionMapper;
import com.mall.promotion.service.TbPromoSessionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xrj
 * @since 2022-05-02
 */
@Service
@Component
@Slf4j
public class TbPromoSessionServiceImpl extends ServiceImpl<TbPromoSessionMapper, TbPromoSession> implements TbPromoSessionService {

    @Autowired
    private TbPromoSessionMapper sessionMapper;

    @Override
    public SecSkillListResp getSecSkillInfo(SecKillListVo secKillListVo) {
        SecSkillListResp resp = new SecSkillListResp();
        SecKillListModel killListModel = new SecKillListModel();
        resp.setSecSkillListModel(killListModel);
        killListModel.setSessionId(secKillListVo.getSessionId());
        //查询场次信息
        QueryWrapper<TbPromoSession> qw = new QueryWrapper<>();
        qw.eq("session_id",secKillListVo.getSessionId());
        qw.eq("enable_date",secKillListVo.getDate());
        try {
            TbPromoSession session = getOne(qw);
            if (session == null){
                resp.setCode(PromotionServiceEnum.SUCCESS.getCode());
                resp.setMsg(PromotionServiceEnum.SUCCESS.getMsg());
                return resp;
            }
            killListModel.setPsId(session.getId());
            List<SecKillProductModel> productModels = sessionMapper.queryProductInfoBySessionId(session.getId());
            killListModel.setProductList(productModels);
            resp.setCode(PromotionServiceEnum.SUCCESS.getCode());
            resp.setMsg(PromotionServiceEnum.SUCCESS.getMsg());
        } catch (Exception e) {
            resp.setCode(PromotionServiceEnum.DATA_QUERY_ERROR.getCode());
            resp.setMsg(PromotionServiceEnum.DATA_QUERY_ERROR.getMsg());
        }
        return resp;
    }
}
